package com.kczd.jinlan.adapter.recycleviewadapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kczd.jinlan.R;
import com.kczd.jinlan.model.Goods;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * zhangy
 * Created by Administrator on 2017/5/22.
 */

public class  RecyclerViewGradviewAdapter extends RecyclerView.Adapter<RecyclerViewGradviewAdapter.ViewHolder> {


    private Context mContext;
    private List<Goods> dataList = new ArrayList<>();


    public void addAllData(List<Goods> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.dataList.clear();
    }

    public RecyclerViewGradviewAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView text_title;
        public TextView text_weight_dai;
        public TextView text_price_kg;
        public TextView text_price;
        public ImageView image_recycler;


        public ViewHolder(View itemView) {
            super(itemView);
            text_title = (TextView) itemView.findViewById(R.id.text_title);
            text_weight_dai = (TextView) itemView.findViewById(R.id.text_weight_dai);
            text_price_kg = (TextView) itemView.findViewById(R.id.text_price_kg);
            text_price = (TextView) itemView.findViewById(R.id.text_price);
            image_recycler = (ImageView) itemView.findViewById(R.id.image_recycler);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false);
        return new ViewHolder(v);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        holder.text_title.setText(dataList.get(position).getTitle());
        holder.text_weight_dai.setText(dataList.get(position).getWeight_dai());
        holder.text_price_kg.setText(dataList.get(position).getPrice_kg());
        holder.text_price.setText(dataList.get(position).getPrice());
        // 显示图片的配置
        DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.img_loading)
                .showImageOnFail(R.mipmap.img_fail).cacheInMemory(true).cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        ImageLoader.getInstance().displayImage(dataList.get(position).getImage(),  holder.image_recycler, options);


    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

}
